package cn.myzqu.sign.user.server.api;


import cn.myzqu.sign.common.response.ResultData;
import cn.myzqu.sign.user.server.entity.OsUser;
import cn.myzqu.sign.user.server.service.UserService;
import cn.myzqu.sign.user.server.utils.TokenUtil;
import cn.myzqu.sign.user.server.vo.UserPublicVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息api
 * @Author: 的川
 * @Date: 2019/1/6.
 */
@RestController
@Slf4j
@RequestMapping()
public class UserApi {

    @Autowired
    private UserService userService;


    @ApiOperation("获取用户公开信息")
    @GetMapping("/info/{id}")
    public ResultData getUserInfo(@PathVariable Integer id){
        //根据id查询用户信息
        OsUser osUser = userService.selectById(id);
        if(osUser!=null){
            UserPublicVo userPublicVo = new UserPublicVo();
            BeanUtils.copyProperties(osUser,userPublicVo);
            return ResultData.success(userPublicVo);
        }
        return ResultData.error("用户不存在");
    }

    @ApiOperation("用户获取个人信息")
    @GetMapping("/info")
    public ResultData getPersonalInfo() throws Exception{
        //根据id查询用户信息
        Integer userId = TokenUtil.getUserId();
        OsUser osUser = userService.selectById(userId);
        if(osUser!=null){
            UserPublicVo userPublicVo = new UserPublicVo();
            BeanUtils.copyProperties(osUser,userPublicVo);
            return ResultData.success(userPublicVo);
        }
        return ResultData.error("用户不存在");
    }

    @ApiOperation("用户完善个人信息")
    @PostMapping("/update")
    public ResultData update(@RequestBody OsUser osUser) throws Exception{
        Integer loginId = TokenUtil.getUserId();
        osUser.setId(loginId);
        if(userService.updateById(osUser)>0){
            return ResultData.success();
        }
        return ResultData.error("完善个人信息失败！");
    }





}
